<?php
/**
 * 微博操作
 * 
 * Created		: 2011-07-06
 * Modified		: 2011-07-06 
 * @link		: http://qixbo.com
 * @copyright	: (C) 2011 Qixbo Inc.
 * @version		: 0.1.0
 * @author		: Joseph Chen (jsph.chen@gmail.com)
 */
class MicroBlog extends Model
{
	
	protected $tbl		= 'mb_log';
	protected $priKey	= 'id';
	
	public $contentLen	= 140;
	public $url2BeLen	= 8;// 每个URL链接统一字符长度
	
	
	/**
	 * 添加一条微博
	 * @param array $data 微博字段数据 
	 */
	public function addBlog($data)
	{
		// 计算内容长度
		$contentLen = mb_strlen($data['content']);
		// 处理微博内容
		if (Url::containUrl($data['content'])) {
			$urls = Url::getUrlsFromStr($data['content']);
			if ($urls) {
				// 重算所有url长度
				foreach ($urls as $v) {
					$contentLen -= (mb_strlen($v) - $this->url2BeLen);
				}
			}
		}
		
		// 内容太长了
		if ($contentLen > $contentLen) {
			$this->errors(
				array(
					'code' => $GLOBALS['CMBContentLengthOverflow'],
					'code' => $GLOBALS['MMBContentLengthOverflow'],
				)
			);
			return false;
		}
		
		foreach ($GLOBALS['mb_plugin'] as $pluginName) {
			$data = call_user_func(array('Mb'.ucfirst($pluginName), 'execute'), $data);
			if (!$data) {
				
			}
		}
		
		$ret = $this->add2Table($data);
		
		if (!$ret) {
			var_export(Db::errors());
		}
		
		return $ret;
	}
}




